/**
 * Created by 25030 on 2016/11/29.
 */
String.prototype.visualLength = function()
{
    var textDiv = $('<div></div>');
    textDiv.addClass('text');
    textDiv.text(this);
    $('body').append(textDiv);
    var width =  textDiv[0].offsetWidth;
    $('body')[0].removeChild(textDiv[0]);
    return width;
};

var messageCoount = 0;



var MessageBox = function(info,type,time){
    if(info == null){
        return;
    }

    messageCoount++;

    var icon = $('<div></div>');
    if(type == "success"){
        icon.append($('<li class="icon-success"></li>'))
    }else if(type == "warning"){
        icon.append($('<li class="icon-warn"></li>'))
    }else if(type == "error"){
        icon.append($('<li class="icon-error"></li>'))
    }else{
        icon.append($('<li class="icon-warn icon-info"></li>'))
    }
    icon.css('float','left');
    icon.css('margin-top','-2px');
    icon.css('margin-right','21px');
    icon.css('width',14);
    icon.css('height',14);

    var messageDiv = $('<div></div>');
    messageDiv.addClass('message');
    messageDiv.animate({ top:50+(messageCoount-1)*45,opacity:1},1000);

    messageDiv.html(info);
    messageDiv.append(icon);
    messageDiv.css('width',info.visualLength()+50)

    $('body').append(messageDiv);

    window.setTimeout(function(){
        messageDiv.animate({ top:-25,opacity:1},1000,function(){
            $('body')[0].removeChild(messageDiv[0]);
            messageCoount--;
        });
    },time || 5000);
};

window.MessageBox = MessageBox;